import sys, os, getopt


def usage():
  print '''usage: python upload.py -i image_file [-u url] [-n name] [-d description] default url is xxx
    '''

def main():
    url = 'http://192.168.195.128:6543/AppWebUpLoadBgImg'
    name = ''
    description = ''
    image_file = ''
    folder = ''
    tel = 'default'
    validtime = '2016-12-31'
    try:
        opts, args = getopt.getopt(sys.argv[1:], "hu:n:d:f:t:", ["help", "image_file=", "url=", "name=", "description=", "tel=", "folder="])
    except getopt.GetoptError, err:
        # print help information and exit:
        print str(err) # will print something like "option -a not recognized"
        usage()
        sys.exit(2)
    for opt, val in opts:
      if opt in ('-h', '--help'):
        usage()
        sys.exit()
      if opt in ('-i', '--image_file'):
        image_file = val
      elif opt in ('-n', '--name'):
        name = val
      elif opt in ('-d', '--description'):
        description = val
      elif opt in ('-u', '--url'):
        url = val
      elif opt in ('-f', '--folder'):
        folder = val
      else:
        usage()
        sys.exit()

    if image_file == '' and folder == '':
      print 'you must specify which file or folder to upload'
      sys.exit()

    if folder != '':
      for image_file in os.listdir(folder):
        cmd = '''curl -F "name={}" -F "tel={}" -F "description={}" -F "image_file=@{}" {}'''.format(name, tel, description, os.path.abspath(folder)+'/'+image_file, url)
        print cmd
        os.system(cmd)
    else:
        cmd = '''curl -F "name={}" -F "tel={}" -F "description={}" -F "image_file=@{}" {}'''.format(name, tel, description, image_file, url)
        print cmd
        os.system(cmd)

if __name__ == '__main__':
    main()
